package com.reebake.ideal.storage;

import com.reebake.ideal.jdbc.FlywayUtil;
import com.reebake.ideal.storage.properties.StorageProperties;
import org.dromara.x.file.storage.spring.EnableFileStorage;
import org.flywaydb.core.Flyway;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackageClasses = StorageAutoConfiguration.class)
@EnableConfigurationProperties(StorageProperties.class)
@EnableFileStorage
public class StorageAutoConfiguration {

    @Bean
    MapperScannerConfigurer storageMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(StorageAutoConfiguration.class.getPackageName() + ".mapper");
        return mapperScannerConfigurer;
    }

    @Bean
    Flyway storageFlyway(DataSource dataSource) {
        String schema = "storage";
        return FlywayUtil.migrate(dataSource, schema);
    }
}
